<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for=" (item,i) in yjlmGuojiaOptions" :label="item.dictLabel" :name="i+''"></el-tab-pane>
    </el-tabs>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="40px">
      <el-form-item label="类目" prop="yjlmTitle">
        <el-input
          v-model="queryParams.yjlmTitle"
          placeholder="请输入类目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['web:sbyjlm:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['web:sbyjlm:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['web:sbyjlm:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['web:sbyjlm:export']"
            >导出
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="addYjlmMethod"
            >复制一级类目
            </el-button>
          </el-col>

<!--          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
        </el-row>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="sbyjlmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="类目" align="center" prop="yjlmTitle"/>
      <el-table-column label="类目编号" align="center" prop="yjlmCode"/>
      <el-table-column label="国家/地区" align="center" prop="yjlmGuojia" :formatter="yjlmGuojiaFormat"/>
      <el-table-column label="类目价格($)" align="center" prop="yjlmPrice"/>
      <el-table-column label="类目介绍中文" align="center" prop="yjlmContentZw"/>
      <el-table-column label="类目介绍英文" align="center" prop="yjlmContentYw"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:sbyjlm:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:sbyjlm:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  <!-- 添加或修改一级类目对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="540px"
    append-to-body
    :close-on-click-modal="false"
    v-dialogDrag
    class="my_dialog">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="类目" prop="yjlmTitle">
        <el-input v-model="form.yjlmTitle" placeholder="请输入类目"/>
      </el-form-item>
      <el-form-item label="类目编号" prop="yjlmTitle">
        <el-input v-model="form.yjlmCode" placeholder="请输入类目编号"/>
      </el-form-item>
      <el-form-item label="国家/地区" prop="yjlmGuojia">
        <el-input v-model="form.yjlmGuojia" placeholder="国家/地区" readonly="readonly"/>
<!--        <el-select v-model="form.yjlmGuojia" placeholder="请选择国家/地区" style="width: 100%;">-->
<!--          <el-option-->
<!--            v-for="dict in yjlmGuojiaOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          ></el-option>-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="类目价格" prop="yjlmPrice">
        <el-input v-model="form.yjlmPrice" placeholder="请输入类目价格"/>
      </el-form-item>
      <el-form-item label="类目介绍中文" prop="yjlmContentZw">
        <el-input v-model="form.yjlmContentZw" type="textarea" placeholder="请输入内容"/>
      </el-form-item>
      <el-form-item label="类目介绍英文" prop="yjlmContentYw">
        <el-input v-model="form.yjlmContentYw" type="textarea" placeholder="请输入内容"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import {listSbyjlm, getSbyjlm, delSbyjlm, addSbyjlm, updateSbyjlm, exportSbyjlm,copyAndInsert} from "@/api/sbxm/sbyjlm/sbyjlm";

export default {
  name: "Sbyjlm",
  components: {},
  data() {
    return {
      activeName:"0",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一级类目表格数据
      sbyjlmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 国家/地区字典
      yjlmGuojiaOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        yjlmGuojia: null,
        yjlmTitle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getDicts("guojiaCode").then(response => {
      this.yjlmGuojiaOptions = response.data;
      this.form.yjlmGuojia = this.yjlmGuojiaOptions[this.activeName].dictValue;
      this.queryParams.yjlmGuojia = this.yjlmGuojiaOptions[this.activeName].dictValue;
      this.getList();
    });
  },
  methods: {
    /** 查询一级类目列表 */
    getList() {
      this.loading = true;
      listSbyjlm(this.queryParams).then(response => {
        this.sbyjlmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    handleClick(e){
      this.activeName = e.index;
      this.queryParams.yjlmGuojia = e.label;
      this.queryParams.yjlmTitle = "";
      this.getList();
      console.log(e);
      console.log(e.name);
    },

    addYjlmMethod(){
      copyAndInsert().then(response => {
        if (response.code === 200) {
          this.msgSuccess("添加成功");
          this.getList();
        }
      });
    },

    // 国家/地区字典翻译
    yjlmGuojiaFormat(row, column) {
      return this.selectDictLabel(this.yjlmGuojiaOptions, row.yjlmGuojia);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        beginTime: null,
        createBy: null,
        createTime: null,
        endTime: null,
        remark: null,
        searchValue: null,
        updateBy: null,
        updateTime: null,
        yjlmContentYw: null,
        yjlmContentZw: null,
        yjlmGuojia: null,
        yjlmPrice: null,
        yjlmTitle: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加一级类目";
      this.form.yjlmGuojia = this.yjlmGuojiaOptions[this.activeName].dictValue;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSbyjlm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改一级类目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSbyjlm(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSbyjlm(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delSbyjlm(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有一级类目数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportSbyjlm(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () {
      });
    }
  }
};
</script>
